import { _decorator, AudioClip, AudioSource, Component, Graphics, Node, RigidBody2D, sp, Vec2 } from 'cc';
import { HpManager } from './HpManager';
const { ccclass, property } = _decorator;

@ccclass('Entity')
export class Entity extends Component {

    /**动画组件 */
    @property({ type: sp.Skeleton })
    public skeleton: sp.Skeleton;
    public rb: RigidBody2D | null = null;

    @property(AudioSource)
    public audioSource: AudioSource = null;


    @property(Number)
    public detectionRadius: number = 5000; // 视野检测范围半径
    @property(Number)
    public attackRadius: number = 50; // 视野检测范围半径
    @property(Number)
    public currentHp: number = 100;
    @property(Number)
    public maxHp: number = 100;

    public detectionArea: Graphics = null;
    public hpBar: HpManager | null = null;

    onLoad() {
        // this.OnDrawGizmos();
        this.hpBar = this.node.getChildByName("HpBar")?.getComponent(HpManager) || null;
    }

    protected start() {
        this.rb = this.getComponent(RigidBody2D);
        this.currentHp = this.maxHp;
    }

    protected update(deltaTime: number) {

    }

    /** 播放指定动画 */
    public playAnimation(animName: string, loop: boolean = true) {
        if (this.skeleton) {
            this.skeleton.setAnimation(0, animName, loop);
        }

    }

    public SetVelocity(_xVelocity: number, _yVelocity: number) {
        this.rb.linearVelocity = new Vec2(_xVelocity, _yVelocity);
    }

    // public OnDrawGizmos(): void {
    //     if (this.detectionArea) return;

    //     this.detectionArea = this.node.addComponent(Graphics);
    //     this.detectionArea.lineWidth = 2;

    //     this.detectionArea.circle(0, 0, this.detectionRadius);
    //     this.detectionArea.stroke();

    // }


    public checkProximity(target: Node, radius: number): boolean {
        const distance = this.node.worldPosition.subtract(target.worldPosition).length();
        return distance <= radius;
    }



}


